/*******************************************************************************
 * Copyright (c) 2010 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package com.googlecode.ssdutils.jlauncher;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;

import com.googlecode.ssdutils.jlauncher.annotations.Launcher;

/**
 * @author Sylvain Sicard
 * 
 */
public class LaunchConfiguration {

	protected Class<?> runner;

	protected Object runnerInstance;

	protected Set<Field> options;

	protected Set<Method> launchers;

	public Class<?> getRunner() {
		return runner;
	}

	public void setRunner(Class<?> runner) throws InstantiationException,
			IllegalAccessException {
		this.runner = runner;
		runnerInstance = runner.newInstance();
	}

	public Set<Field> getOptions() {
		return options;
	}

	public void setOptions(Set<Field> options) {
		this.options = options;
	}

	public Set<Method> getLaunchers() {
		return launchers;
	}

	public void setLaunchers(Set<Method> launchers) {
		this.launchers = launchers;
	}

	/**
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * 
	 */
	public void run(String command) throws IllegalArgumentException,
			IllegalAccessException, InvocationTargetException {
		for (Method launcher : launchers) {
			Launcher launcherAnnotation = launcher
					.getAnnotation(Launcher.class);
			if (launcherAnnotation.command().equals(command)) {
				launcher.invoke(runnerInstance);
			}
		}
	}

	/**
	 * @param f
	 * @param optionValue
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	public void setOptionValue(Field f, String optionValue)
			throws IllegalArgumentException, IllegalAccessException {
		f.setAccessible(true);
		Class<?> type = f.getType();

		if (type.getName().equals("int")) {
			f.setInt(runnerInstance, Integer.parseInt(optionValue));
		} else if (type.getName().equals("long")) {
			f.setLong(runnerInstance, Long.parseLong(optionValue));
		} else if (type.getName().equals("float")) {
			f.setFloat(runnerInstance, Float.parseFloat(optionValue));
		} else if (type.getName().equals("double")) {
			f.setDouble(runnerInstance, Double.parseDouble(optionValue));
		} else if (type.getName().equals("boolean")) {
			f.setBoolean(runnerInstance, Boolean.parseBoolean(optionValue));
		} else if (type.getName().equals("short")) {
			f.setShort(runnerInstance, Short.parseShort(optionValue));
		} else if (type.getName().equals("char")) {
			f.setChar(runnerInstance, optionValue.charAt(0));
		} else {
			f.set(runnerInstance, optionValue);
		}
	}

}
